package com.xiaoerkeji.eshop.plug;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.xiaoerkeji.eshop.R;
import com.xiaoerkeji.eshop.MainActivity;
import com.lt.app.ResHelper;
import com.lt.common.ConvertHelper;

import androidx.annotation.DrawableRes;

/**
 * 标题栏辅助类
 */
public class TopBarHelper implements View.OnClickListener
{

    private ImageView iv_bar_left;
    private ImageView iv_bar_right;
    private TextView tv_bar_title;
    private final Activity mActivity;
    private onBarClickBack mBarClickBack;
    private View mView;

    public interface onBarClickBack
    {
        void onLeft();

        void onRight();
    }

    public TopBarHelper(Activity act, View v)
    {
        this.mActivity = act;
        init(v);
    }

    private void init(View v)
    {
        if (v != null)
        {
            mView = v;
            iv_bar_left = v.findViewById(R.id.iv_bar_left);
            iv_bar_right = v.findViewById(R.id.iv_bar_right);
            tv_bar_title = v.findViewById(R.id.tv_bar_title);
            if (iv_bar_left != null)
            {
                iv_bar_left.setOnClickListener(this);
            }
            if (iv_bar_right != null)
            {
                iv_bar_right.setOnClickListener(this);
            }
        }
    }


    public void setTitle(Object t)
    {
        if (tv_bar_title != null && t != null)
        {
            if (t instanceof Integer)
            {
                tv_bar_title.setText((int) t);
            } else
            {
                tv_bar_title.setText(ConvertHelper.getInstance().getStr(t));
            }
//            if (BuildConfig.DEBUG)
//            {
//                tv_bar_title.append(ResUtils.getString(R.string.debug_ver));
//            }
        }
    }

    public void setTitle(Object title, int color)
    {
        if (tv_bar_title == null)
        {
            return;
        }
        if (title != null)
        {
            if (title instanceof Integer)
            {
                tv_bar_title.setText((int) title);
            } else
            {
                tv_bar_title.setText(ConvertHelper.getInstance().getStr(title));
            }
        }
        if (color == 0)
        {
            tv_bar_title.setTextColor(ResHelper.getInstance().getColor(R.color.black_2));
        } else
        {
            tv_bar_title.setTextColor(color);
        }
    }

    public void setTitleColor(int color)
    {
        if (tv_bar_title != null)
        {
            tv_bar_title.setTextColor(color);
        }
    }

    public TextView getTitleTv()
    {
        return tv_bar_title;
    }

    public void setCallBack(onBarClickBack callback)
    {
        this.mBarClickBack = callback;
    }

    public void setLeftVisibility(int visibility)
    {
        if (iv_bar_left != null)
        {
            iv_bar_left.setVisibility(visibility);
        }
    }

    public void setRightVisibility(int visibility)
    {
        if (iv_bar_right != null)
        {
            iv_bar_right.setVisibility(visibility);
        }
    }

    public void setLeftImage(@DrawableRes int resId)
    {
        if (iv_bar_left != null)
        {
            iv_bar_left.setImageResource(resId);
        }
    }

    public void setRightImage(@DrawableRes int resId)
    {
        if (iv_bar_right != null)
        {
            iv_bar_right.setImageResource(resId);
        }
    }

    /**
     * 设置背景颜色
     */
    public void setBg(int color)
    {
        if (mView != null)
        {
            mView.setBackgroundColor(color);
        }
    }


    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View v)
    {
        switch (v.getId())
        {
            case R.id.iv_bar_left:
                if (mBarClickBack == null)
                {
                    mActivity.finish();
                } else
                {
                    mBarClickBack.onLeft();
                }
                break;
            case R.id.iv_bar_right:
                if (mBarClickBack == null)
                {
                    Intent intent = new Intent(mActivity, MainActivity.class);
                    mActivity.startActivity(intent);
                } else
                {
                    mBarClickBack.onRight();
                }
                break;
        }
    }
}
